#include "MenuSystem.h"
#include "IrControl.h"
#include <fcntl.h>
#include "Menu/Tool/frame.h"
#include "BF533/TouchPanel.h"
#include "BF533/FreadWrite.h"

#define ITEM_DISPADJ_BRIGHT_DOWN	(0)
#define ITEM_DISPADJ_BRIGHT_UP	(1)
#define ITEM_DISPADJ_COLOR_DOWN	(2)
#define ITEM_DISPADJ_COLOR_UP	(3)
#define ITEM_DISPADJ_HUE_DOWN	(4)
#define ITEM_DISPADJ_HUE_UP	(5)
#define ITEM_DISPADJ_EXIT		(6)

#define DISPADJ_BUTTON_QTY   (7)
#define VIDEO_BAR_DISTANCE 28

#define I2C_SLAVE	0x0703	/* Change slave address			*/
#define I2C_SLAVE_bitek0	0x00
#define I2C_SLAVE_bitek1	0x01

TRect		VideoDispAdjTouchRect[DISPADJ_BUTTON_QTY];
TPosCoor		VideoDispAdjItemPos[DISPADJ_BUTTON_QTY] = { {173,88}, {585,88}, {173,182}, {585,182}, {173,272}, {585,272}, {576,376} };
TRect		VideoBRIInfoPos= {5,88,159,176};
TRect		VideoCOLInfoPos= {5,181,159,269};
TRect		VideoHUEInfoPos= {5,272,159,360};
TPosCoor		VideoBRIBarPos= {284,102};
TPosCoor		VideoCOLBarPos= {284,192};
TPosCoor		VideoHUEBarPos= {284,286};
TRect		Touch_clbr = {0,0,720,65}; //////20080217
int		VideoDispAdjFocus = -1;
TFrame		VideoDispAdjFrame;
char		VideoDispAdjInfoStr[40];
static unsigned char 		BrightValue = 5;
static unsigned char 		ColorValue = 5;
static unsigned char		Huevalue = 5;
//unsigned char video_data [5] = {0xe0,0x03,0x08,0x00,0xEB};
extern  unsigned char send_data_buf[5];
//unsigned char touch_clbr_flag = 0; ///20080217
extern	unsigned char read_info_data[10];
extern  TRect	  MuteInfoPos;
extern int volume_count ;
extern int volume_temp;
extern unsigned char mute_status;
extern unsigned char	VOLvalue ;
extern struct _VolumePos VolumePos;
extern TRect	BarInfoRect;
extern unsigned char rece_handle;	
extern unsigned char dtpresent_flag;
extern pthread_cond_t dt_cond;
extern int TS_KX; ////20080217
extern int TS_KY;
extern int TP_CLBR_C_LX ;//logic x
extern int TP_CLBR_C_LY ;
unsigned char Bri_tab[11] = {0,30,60,80,98,108,118,130,148,170,200};
unsigned char Col_tab[11] = {10,30,60,72,88,100,110,120,135,165,195};
unsigned char Con_tab[11] = {10,30,40,50,60,69,74,78,82,86,90};
char*		VideoDispAdjInfo = { "END"};


int LCDTWI_control ( int command)
{
		int i2c_fd;
    int slave_addr;
    int value; 
    unsigned char   buf[2];
	
		i2c_fd = open("/dev/i2c-0", O_RDWR);
		if(ioctl(i2c_fd, I2C_SLAVE, I2C_SLAVE_bitek0)<0)
    {
	    printf("Fail to set SLAVE address\n");
	    return -1;
    }
    
    switch (command)
    {
    	case 	0x01://bright
    		slave_addr = 0x7c;
    		value =BrightValue;
    		buf[0] = slave_addr;
    			buf[1] = Bri_tab[value];//////////20080218
    		if (write(i2c_fd, buf, 2 ) != 2 )
    			printf ("write %d ,%d error!\n", buf[0], buf[1]);    	
   				buf[0] = slave_addr+1;
    		if (write(i2c_fd, buf, 2 ) != 2 )
    			printf ("write %d ,%d error!\n", buf[0], buf[1]); 
   				buf[0] = slave_addr+2;
    		if (write(i2c_fd, buf, 2 ) != 2 )
    			printf ("write %d ,%d error!\n", buf[0], buf[1]); 
    				break;
    	case 	0x02://color
    		slave_addr = 0x7f;//0x7c; 
    		value = ColorValue;	
    		buf[0] = slave_addr;
    		buf[1] = Col_tab[value]; 
    		if (write(i2c_fd, buf, 2 ) != 2 )
    			printf ("write %d ,%d error!\n", buf[0], buf[1]);    	
   				buf[0] = slave_addr+1;
    		if (write(i2c_fd, buf, 2 ) != 2 )
    			printf ("write %d ,%d error!\n", buf[0], buf[1]); 
   				buf[0] = slave_addr+2;
    		if (write(i2c_fd, buf, 2 ) != 2 )
    			printf ("write %d ,%d error!\n", buf[0], buf[1]);
    		break;
    	case 	0x03://contrast
    		slave_addr = 0x8e;//0x7F; 
    		value = Huevalue+1;//avoid to full black
    		buf[0] = slave_addr;
    		buf[1] = Con_tab[value];
    		if (write(i2c_fd, buf, 2 ) != 2 )
    			printf ("write %d ,%d error!\n", buf[0], buf[1]);  
   		
    		break;    		
    }  
    //////////////close osd //////////////
    if(ioctl(i2c_fd, I2C_SLAVE, I2C_SLAVE_bitek1)<0)
    {
	    printf("Fail to set SLAVE address\n");
	    return -1;
    }
    	slave_addr = 0x51; 
    	value = 0x0c;
    	buf[0] = slave_addr;
    	buf[1] = value;
    	if (write(i2c_fd, buf, 2 ) != 2 )
    			printf ("write %d ,%d error!\n", buf[0], buf[1]);  
   
/*    buf[0] = slave_addr;
    buf[1] = value*20;    
  
    if (write(i2c_fd, buf, 2 ) == 2 )
    	printf ("write %d ,%d ok  ", buf[0], buf[1]);    	
   buf[0] = slave_addr+1;
    if (write(i2c_fd, buf, 2 ) == 2 )
    	printf ("write %d ,%d ok  ", buf[0], buf[1]); 
   buf[0] = slave_addr+2;
    if (write(i2c_fd, buf, 2 ) == 2 )
    	printf ("write %d ,%d ok  \n", buf[0], buf[1]); 
 */   	    	
    close(i2c_fd);
    return 0;	
}

int  VideoDispUpdata (void)
{
return 0;
}


int  VideoAdjInfo()
{
		int value;
	
		value = read_info_data[4] - 1;
		switch (read_info_data[3])
		{
			case	ID_SCREEN_INFO_BRI:		
				if (( value >10)|| (value <0) )
					value = 5;		
				BrightValue = value;
				//printf ("bri: %d,\n",BrightValue);
				LCDTWI_control(1);
				break;
			case  ID_SCREEN_INFO_CLOR:
				if (( value >10) || (value <0) )
					value = 5;	
				ColorValue = value;				
				//printf ("col: %d,\n",ColorValue);
				LCDTWI_control(2);
				break;
			case  ID_SCREEN_INFO_HUE:
				if (( value >10)|| (value <0) )
					value = 5;	
				Huevalue = value;				
				//printf ("hue: %d,\n",Huevalue);
				LCDTWI_control(3);
				break;
			}
			
			return 0;
}



void VideoDispAdjDrawBar( TCanvas *pCanvas, int idx)
{
	char FileName[20];
	int GetJpegIdx; 
	TPosCoor	 pos;
	int value=0,i;

		
	switch(idx)
	{
		case ITEM_DISPADJ_BRIGHT_UP:
		case ITEM_DISPADJ_BRIGHT_DOWN:			
			pos.x = VideoBRIBarPos.x;
			pos.y = VideoBRIBarPos.y;
			value = BrightValue;
			break;
			
		case ITEM_DISPADJ_COLOR_UP:
		case ITEM_DISPADJ_COLOR_DOWN:
			pos.x = VideoCOLBarPos.x;
			pos.y = VideoCOLBarPos.y;
			value = ColorValue;
			break;	
		case ITEM_DISPADJ_HUE_UP:
		case ITEM_DISPADJ_HUE_DOWN:
			pos.x = VideoHUEBarPos.x;
			pos.y = VideoHUEBarPos.y;
			value = Huevalue;
			break;	
	}

	
	for ( i=0;  i<11; i++)
		{	
		if ( i==value)
			GetJpegIdx = IMG_VIDEO_DISPADJ_HLBAR ;
	    	else
			GetJpegIdx = IMG_VIDEO_DISPADJ_DEFBAR;
			sprintf( FileName, "res_jpeg/%d/%d.jpg", VIDEO_DISPADJ_FOLDER, GetJpegIdx);
			DrawJpeg(pCanvas, pos.x+VIDEO_BAR_DISTANCE*i,  pos.y, FileName);	
		}
	GetJpegIdx = IMG_VIDEO_DISPADJ_MIDBAR ;
	sprintf( FileName, "res_jpeg/%d/%d.jpg", VIDEO_DISPADJ_FOLDER, GetJpegIdx);
	DrawJpeg(pCanvas, pos.x+VIDEO_BAR_DISTANCE*5,  pos.y, FileName);	
	//printf ("in VideoDispAdjDrawBar value is %d ,idx is %d\n",value,idx);
}

void VideoDispAdjDrawVolume( TCanvas *pCanvas,int flag)
{
	int GetJpegIdx;
	TRect rect;
	TJpeg pJpeg;
	char FileName[20];
	int x,y;
	int i;
	switch (flag)
		{							
			case 1://disp volume all
				GetJpegIdx = IMG_VOLUME_BARCOVER;
				sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);	
				DrawJpeg(pCanvas, VolumePos.Volume_all.x, VolumePos.Volume_all.y, FileName);	
				if (mute_status == 1)
					{
						GetJpegIdx = IMG_VOLUME_MUTE ;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						DrawJpeg(pCanvas, VolumePos.Volume_speaker.x, VolumePos.Volume_speaker.y, FileName);	
					}				
				else
						mute_status = 2;
				
				for (i = 0; i<VOLvalue; i++)
					{
					if (i%2)
						{
						x = VolumePos.Volume_bar_start.x+VolumePos.Distance*(i/2) + VolumePos.Barwidth;
						GetJpegIdx = IMG_VOLUME_RIGHTFOCUS;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						}
					else
						{
						x = VolumePos.Volume_bar_start.x+VolumePos.Distance*(i/2) ;
						GetJpegIdx = IMG_VOLUME_LEFTFOCUS;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						}
					y = VolumePos.Volume_bar_start.y;					
					DrawJpeg(pCanvas,x, y, FileName);						
					}
			break;			
			
			case 2://draw bar
			
			break;
			
			case 3://undisp
				DrawJpeg(pCanvas, 0, 8, "/res_jpeg/bar.jpg" );
				g_Puts( pCanvas, FONT33, BarInfoRect, TEXT_ALIGN_CENTER, "DISPLAY  ADJUST" );
			break;
		}
	
	pJpeg = GetJpegPtr("/res_jpeg/bar.jpg" );
	rect.left = BARPOS_X;
	rect.right = rect.left + pJpeg.Width;
	rect.top = BARPOS_Y;
	rect.bottom = rect.top + pJpeg.Height;	
	dt_Present_part(rect);		
	volume_count = 0;
	volume_temp = 0;

}


static void VolumeCountHandle(void)
{
	volume_count ++;
		if (volume_count >0xFF00)
			{
				volume_temp ++;
				volume_count = 0;
			}
		if (volume_temp > 10)
			{
				VideoDispAdjDrawVolume(VideoDispAdjFrame.pCanvas,3);					
				volume_count = 0;	
				volume_temp = 0;	
				mute_status =0;
			}
}

void VideoDispAdjDrawInfo( TCanvas *pCanvas, int idx)
{
	TJpeg pJpeg;
	TRect rect; 
	char FileName[20];
	int GetJpegIdx; 

	g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_WHITE, 0, 0 );
	switch(idx)
	{
		case ITEM_DISPADJ_BRIGHT_UP:
		case ITEM_DISPADJ_BRIGHT_DOWN:			
			GetJpegIdx = IMG_VIDEO_DISPADJ_BRI_DISPCOVER ;
			sprintf( FileName, "res_jpeg/%d/%d.jpg", VIDEO_DISPADJ_FOLDER, GetJpegIdx);	
			pJpeg = GetJpegPtr(FileName);
			rect.left = VideoBRIInfoPos.left;
			rect.top = VideoBRIInfoPos.top;				
			sprintf(VideoDispAdjInfoStr, "BRI %d", BrightValue);	
			g_Puts( pCanvas, FONT33, VideoBRIInfoPos, TEXT_ALIGN_CENTER, VideoDispAdjInfoStr);//MenuRadioFMChannelFreq[FocusIdx] );
			VideoDispAdjDrawBar(pCanvas,idx);
			break;
			
		case ITEM_DISPADJ_COLOR_UP:
		case ITEM_DISPADJ_COLOR_DOWN:
			GetJpegIdx = IMG_VIDEO_DISPADJ_COL_DISPCOVER;
			sprintf( FileName, "res_jpeg/%d/%d.jpg", VIDEO_DISPADJ_FOLDER, GetJpegIdx);	
			pJpeg = GetJpegPtr(FileName);
			rect.left = VideoCOLInfoPos.left;
			rect.top = VideoCOLInfoPos.top;			
			sprintf(VideoDispAdjInfoStr, "COL %d", ColorValue);
			g_Puts( pCanvas, FONT33, VideoCOLInfoPos, TEXT_ALIGN_CENTER, VideoDispAdjInfoStr);//MenuRadioFMChannelFreq[FocusIdx] );
			VideoDispAdjDrawBar(pCanvas,idx);
			break;	
		case ITEM_DISPADJ_HUE_UP:
		case ITEM_DISPADJ_HUE_DOWN:
			GetJpegIdx = IMG_VIDEO_DISPADJ_HUE_DISPCOVER;
			sprintf( FileName, "res_jpeg/%d/%d.jpg", VIDEO_DISPADJ_FOLDER, GetJpegIdx);	
			pJpeg = GetJpegPtr(FileName);
			rect.left = VideoHUEInfoPos.left;
			rect.top = VideoHUEInfoPos.top;			
			sprintf(VideoDispAdjInfoStr, "HUE %d", Huevalue);
			g_Puts( pCanvas, FONT33, VideoHUEInfoPos, TEXT_ALIGN_CENTER, VideoDispAdjInfoStr);//MenuRadioFMChannelFreq[FocusIdx] );
			VideoDispAdjDrawBar(pCanvas,idx);
			break;	
	}
	
	rect.right = rect.left + pJpeg.Width;	
	rect.bottom = rect.top + pJpeg.Height;
	DrawJpeg(pCanvas, rect.left, rect.top, FileName);		
	g_Puts( pCanvas, FONT33, rect, TEXT_ALIGN_LEFT, VideoDispAdjInfoStr );
	

}

/*
void VideoDispAdjDrawInfo( TCanvas *pCanvas, int idx)
{
	TRect rect;
	TJpeg pJpeg;
	char FileName[20];
	int GetJpegIdx;

	g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_WHITE, 0, 0 );
	
	GetJpegIdx = IMG_VIDEO_DISPADJ_DISPCOVER ;
	sprintf( FileName, "res_jpeg/%d/%d.jpg", VIDEO_DISPADJ_FOLDER, GetJpegIdx);	
	pJpeg = GetJpegPtr(FileName);

	rect.left = 20;
	switch(idx)
	{
		case ITEM_DISPADJ_BRIGHT_UP:
		case ITEM_DISPADJ_BRIGHT_DOWN:
			rect.top = 98;
			sprintf(VideoDispAdjInfoStr, "BRI %d", BrightValue);
			break;
		case ITEM_DISPADJ_COLOR_UP:
		case ITEM_DISPADJ_COLOR_DOWN:
			rect.top = 228;
			sprintf(VideoDispAdjInfoStr, "CON %d", ColorValue);
			break;
		case ITEM_DISPADJ_HUE_UP:
		case ITEM_DISPADJ_HUE_DOWN:
			rect.top = 359;
			sprintf(VideoDispAdjInfoStr, "HUE %d", Huevalue);
			break;		
	}
	rect.right = rect.left + pJpeg.Width;
	rect.bottom = rect.top + pJpeg.Height;

	DrawJpeg(pCanvas, 0, rect.top, FileName);
	
	g_FillRect(pCanvas, 175, 120, 190, 170, _RGB(255,255,255));

	g_Puts( pCanvas, FONT33, rect, TEXT_ALIGN_LEFT, VideoDispAdjInfoStr );
}

*/
void VideoDispAdjDrawBackgroup(TCanvas *pCanvas)
{
	DrawJpeg(pCanvas, 0, 0, "/res_jpeg/bkg.jpg" );
	DrawJpeg(pCanvas, 0, 8, "/res_jpeg/bar.jpg" );
	g_Puts( pCanvas, FONT33, BarInfoRect, TEXT_ALIGN_CENTER, "DISPLAY  ADJUST" );

}

void VideoDispAdjDrawItem( TCanvas *pCanvas, int idx, BOOL bfocus )
{
	TJpeg pJpeg;
	TPosCoor pos;
	int tmpid;
	char FileName[20];
	int GetJpegIdx;

	if( idx < 0 || idx >= DISPADJ_BUTTON_QTY )
		return;
	g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_WHITE, 0, 0 );	
        if(idx < 6)
        	tmpid = idx % 2;
        else
        	tmpid = 2;
        
	if( bfocus ){
		GetJpegIdx = IMG_VIDEO_DISPADJ_ITEM_FOCUS_LEFT+tmpid;
	}else{
		GetJpegIdx = IMG_VIDEO_DISPADJ_ITEM_LEFT+tmpid;
	}
	pos = VideoDispAdjItemPos[idx];
	sprintf( FileName, "res_jpeg/%d/%d.jpg", VIDEO_DISPADJ_FOLDER, GetJpegIdx);	

	pJpeg = GetJpegPtr(FileName);
	DrawJpeg(pCanvas, pos.x, pos.y, FileName);
	
	VideoDispAdjTouchRect[idx].left = pos.x;
	VideoDispAdjTouchRect[idx].right = pos.x + pJpeg.Width;
	VideoDispAdjTouchRect[idx].top = pos.y;
	VideoDispAdjTouchRect[idx].bottom = pos.y + pJpeg.Height;
	if (idx == ITEM_DISPADJ_EXIT)
	g_Puts( pCanvas, FONT33, VideoDispAdjTouchRect[idx], TEXT_ALIGN_CENTER, VideoDispAdjInfo );	
	
}

void VideoDispAdjDraw( BOOL bDrawAll )
{
	int i;
	if( bDrawAll )
		VideoDispAdjDrawBackgroup(VideoDispAdjFrame.pCanvas);
	for( i = 0; i < DISPADJ_BUTTON_QTY; i++ )
		VideoDispAdjDrawItem( VideoDispAdjFrame.pCanvas, i, FALSE );
	VideoDispAdjDrawInfo( VideoDispAdjFrame.pCanvas, ITEM_DISPADJ_BRIGHT_UP);
	VideoDispAdjDrawInfo( VideoDispAdjFrame.pCanvas, ITEM_DISPADJ_COLOR_UP);
	VideoDispAdjDrawInfo( VideoDispAdjFrame.pCanvas, ITEM_DISPADJ_HUE_UP);
	if (mute_status == 1)
		{
			VideoDispAdjDrawVolume(VideoDispAdjFrame.pCanvas,1);
		}
	dtpresent_flag++;		
	pthread_cond_signal(&dt_cond);
	//dt_Present();
}
/*
void Disp_TouchCalibrate (void)
{
	
	DrawJpeg(VideoDispAdjFrame.pCanvas, 0, 0, "/res_jpeg/touch_clbr.jpg" );
	dtpresent_flag++;		
	pthread_cond_signal(&dt_cond);	
	//dt_Present();	
	touch_clbr_flag = 1;
	memset(Touch_clbr_pos, 0, sizeof(Touch_clbr_pos));	
	TS_KX = 100;
	TS_KY = 100;
}
*/
/*int TouchCalibrate (TPosCoor pos)
{	
	int offer_x,offer_y;
	int temp_x,temp_y;
	long temp_kx,temp_ky;
	
	temp_x = (pos.x <100) ? 60:660;
	temp_y = (pos.y <100) ? 60:420;			
	
	if ( (Touch_clbr_pos[0].x != 0) && (pos.x>100 ) && (pos.x<400 ))
	{
		temp_x = 360;
		temp_y = 240;			
	}
	 				
	offer_x = ((pos.x-temp_x >0) ? (pos.x-temp_x) : (-1 *(pos.x-temp_x)) ) ;
	offer_y = ((pos.y-temp_y >0) ? (pos.y-temp_y) : (-1 *(pos.y-temp_y)) );
		
	
	
	if ((offer_x < TP_CLBR_OFFER ) && (offer_x < TP_CLBR_OFFER) )
	 {
	 		switch (temp_x)
	 		{
	 			case 60:
	 				if (pos.y <100)
	 					{
	 						Touch_clbr_pos[0].x = pos.x;
	 						Touch_clbr_pos[0].y = pos.y;
	 					}
	 				else
	 					{
	 						Touch_clbr_pos[2].x = pos.x;
	 						Touch_clbr_pos[2].y = pos.y;
	 						TP_CLBR_C_LX = pos.x;
	 						TP_CLBR_C_LY = pos.y;
	 					}
	 				break;
	 			case 660:
	 				if (pos.y <100)
	 					{
	 						Touch_clbr_pos[1].x = pos.x;
	 						Touch_clbr_pos[1].y = pos.y;
	 					}
	 				else
	 					{
	 						Touch_clbr_pos[3].x = pos.x;
	 						Touch_clbr_pos[3].y = pos.y;
	 					}
	 				break;
	 			case 360:	 				
	 						Touch_clbr_pos[4].x = pos.x;
	 						Touch_clbr_pos[4].y = pos.y;	 					
	 				break;
	 		} 	 	
	 	}
	 	else
	 		{
	 			printf ("Too much offer!!\n");
	 			return 0;		
	 		}
	 		
	 	if ( Touch_clbr_pos[4].x != 0)
	 	{		
	 		temp_kx = ((Touch_clbr_pos[1].x-Touch_clbr_pos[0].x)*100/(TP_CLBR_B_X-TP_CLBR_A_X)+(Touch_clbr_pos[3].x-Touch_clbr_pos[2].x)*100/(TP_CLBR_D_X-TP_CLBR_C_X))/2;
	 		temp_ky = ((Touch_clbr_pos[2].y-Touch_clbr_pos[0].y)*100/(TP_CLBR_C_Y-TP_CLBR_A_Y)+(Touch_clbr_pos[3].y-Touch_clbr_pos[1].y)*100/(TP_CLBR_D_Y-TP_CLBR_B_Y))/2;
	 		temp_x = Touch_clbr_pos[2].x + (temp_kx*(TP_CLBR_E_X - TP_CLBR_C_X))/100;
	 		temp_y = Touch_clbr_pos[2].y + (temp_ky*(TP_CLBR_E_Y - TP_CLBR_C_Y))/100;
	 		
			offer_x = (Touch_clbr_pos[4].x-temp_x >0) ? (Touch_clbr_pos[4].x-temp_x) : (-1 *(Touch_clbr_pos[4].x-temp_x)) ;
			offer_y = (Touch_clbr_pos[4].y-temp_y >0) ? (Touch_clbr_pos[4].y-temp_y) : (-1 *(Touch_clbr_pos[4].y-temp_y)) ;

			if ((offer_x < TP_CLBR_OFFER ) && (offer_x < TP_CLBR_OFFER) )
			{
				printf ("  center is calulate ok!!\n");
				printf ("temp_kx: %d ,temp_ky is: %d temp_x: %d,temp_y: %d\n",temp_kx,temp_ky,temp_x,temp_y);
				TS_KX = (temp_kx > TP_KXY_LI) ? temp_kx : TS_KX;
					
				if ( send_tp_data_out (1))
					printf ("write failure !\n");	
					
				TS_KY = (temp_ky > TP_KXY_LI) ? temp_ky : TS_KY;
				memset(Touch_clbr_pos, 0, sizeof(Touch_clbr_pos));	
				touch_clbr_flag = 0;
				MenuVideoDraw( TRUE );				
				if ( send_tp_data_out (2))
					printf ("write failure !\n");	
				WriteToText();
				return 1;
			}	
			else
				{
				printf ("  center is calulate error!!\n");						
				}			
		}
		return 0;	
}
*/

int VideoDispAdjGetTouchID( TPosCoor pos )
{
	int i;
	//static char j = 0;
///////////////////	20080217   for  touchscreen calibrate ///////////
/*	if ( (ut_PtInRect( pos, Touch_clbr)) || touch_clbr_flag  )
		{
			if (j!=0)
				{
					if ( !touch_clbr_flag)
						Disp_TouchCalibrate();
					else
						{					
							if (TouchCalibrate(pos))
								{
									j = 0;
									return -1;	
								}				
						}
				}
				j++;
			return -1;
		}		
	else
	*/
		{	
			
		for( i = 0; i < DISPADJ_BUTTON_QTY; i++ )
			if( ut_PtInRect( pos, VideoDispAdjTouchRect[i] ) )
				return i;
		}
	return -1;
}

void VideoDispAdjBtnProc(int idx)
{
	switch(idx)
	{
		case ITEM_DISPADJ_BRIGHT_UP:
				LCDTWI_control(1);
			break;
		case ITEM_DISPADJ_BRIGHT_DOWN:
				LCDTWI_control(1);
			break;
		case ITEM_DISPADJ_COLOR_UP:
				LCDTWI_control(2);
			break;
		case ITEM_DISPADJ_COLOR_DOWN:
				LCDTWI_control(2);
			break;
		case ITEM_DISPADJ_HUE_UP:
				LCDTWI_control(3);
			break;		
		case ITEM_DISPADJ_HUE_DOWN:
				LCDTWI_control(3);
			break;
	}
	VideoDispAdjDrawItem( VideoDispAdjFrame.pCanvas, idx, FALSE );	
	VideoDispAdjDrawInfo( VideoDispAdjFrame.pCanvas, idx);
	dtpresent_flag++;		
	pthread_cond_signal(&dt_cond);
	//dt_Present();
	//dt_Present_part(MenuVideoTouchRect [idx]);
}

BOOL VideoDispAdjBtnEnter(int idx)
{
	switch(idx)
	{
		case ITEM_DISPADJ_BRIGHT_UP:
		case ITEM_DISPADJ_BRIGHT_DOWN:
		case ITEM_DISPADJ_COLOR_UP:
		case ITEM_DISPADJ_COLOR_DOWN:
		case ITEM_DISPADJ_HUE_UP:
		case ITEM_DISPADJ_HUE_DOWN:
			VideoDispAdjBtnProc(idx);
			break;
		case ITEM_DISPADJ_EXIT:
			MenuMainInit();
			break;
	}
	LOG( "btn = %d\n", idx );	

	return TRUE;
}


void VideoDispInit(void)
{
	ir_SetCurrentID( IR_VIDEO_DISPADJ );
	//touch_clbr_flag = 0;
	frm_Create( &VideoDispAdjFrame, FRM_NO_TITLE|FRM_NO_TASK, "" );
	VideoDispAdjDraw( TRUE );
	
}
/*****************************
function:message_dispatch

 return:
  0: successful
  1: need other menu handle
  -1:failure
*****************************/
static int message_dispatch (void)
{
	int rc = 1;
	
	switch (read_info_data[2])
	{
					case	ID_POWER:						
						break;
						
					case	ID_DISPLAY_SCREEN:	
							switch (read_info_data[3])
								{
									case 	ID_SCREEN_MENU:
											MenuMainInit();
										break;		
									case 	ID_SCREEN_RADIO:
											MenuRadioInit();
										break;									
									case	ID_SCREEN_CDLOAD:											
									case	ID_SCREEN_CDDISP:
									case	ID_SCREEN_MP3DISP:
									case	ID_SCREEN_NODISC:
									case	ID_SCREEN_DISCERR:
									case	ID_SCREEN_EJECT:
											MenuCdInit();
											rece_handle = 0;
										break;
									case	ID_SCREEN_MEDIEA:
											MenuMediaInit();
										break;
									
									case 	ID_SCREEN_NAVI:
										MenuNaviInit();
										break;	
										
									case	 	ID_SCREEN_AUDIODISP:
											MenuAudioInit();
											break;	
									case		ID_SCREEN_VIDEODISP:
										MenuVideoInit();
										break;	
									case		ID_SCREEN_EQDISP:
											AudioEQInit();
										break;	
									case	 	ID_SCREEN_BASTRA:
											AudioBASInit();
										break;	
									case		ID_SCREEN_FADBAL:	
											AudioFADInit();							
										break;	
									}
									rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;											
						break;
						
					case	ID_KEY_STATUS:						
						break;
						
					case	ID_SCREEN_INFO:
						if (read_info_data[3] == ID_SCREEN_INFO_VOLUME)
								{
									VideoDispAdjDrawVolume(VideoDispAdjFrame.pCanvas,1);
								}
									
								break;
						
					case	ID_KEYPAD:	
						switch (read_info_data[3])
							{
									case 	ID_MUTE:
										VideoDispAdjDrawVolume(VideoDispAdjFrame.pCanvas,1);
										break;
									case	ID_DMUTE:	
										VideoDispAdjDrawVolume(VideoDispAdjFrame.pCanvas,3);
										break;									
									case 	ID_SEEKUP:
									case 	ID_SEEKDN:
										break;
										
									case 	ID_DEST:
									case 	ID_NAVI:
										MenuNaviInit();
										break;
										
									case 	ID_FMAM:
										MenuRadioInit();
										break;
									case 	ID_CD:
										MenuCdInit();
										break;				
									case 	ID_MEDIA:
										MenuMediaInit();								
										break;
									case 	ID_MENU:
										MenuMainInit();
										break;
										
									case	ID_TVAUX:
										
										break;
							}	
							rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;
						break;
					case 	ID_BAND_SEL:
							MenuRadioInit();
							rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;
						break;	
						
					default:	
							rc = -1;						
						}	
	return 0;
}

/*
static int send_data_out ( int index)
{
	int value=0,i=0;
	if ((index < 0) || (index > DISPADJ_BUTTON_QTY))
		return -1;
		if (index != 6)
			{
				switch(index)
				{
					case ITEM_DISPADJ_BRIGHT_UP:
						if(BrightValue < 10)
						BrightValue++;
						value = BrightValue+1;
							i = 0;
						break;
					case ITEM_DISPADJ_BRIGHT_DOWN:
						if(BrightValue)
							BrightValue--;
							value = BrightValue+1;
							i = 0;
						break;
					case ITEM_DISPADJ_COLOR_UP:
						if(ColorValue < 10)
							ColorValue++;
							value = ColorValue+1;
							i = 1;
						break;
					case ITEM_DISPADJ_COLOR_DOWN:
						if(ColorValue)
							ColorValue--;
							value = ColorValue+1;
							i = 1;
						break;
					case ITEM_DISPADJ_HUE_UP:
						if(Huevalue < 10)
							Huevalue++;
							value = Huevalue+1;
							i = 2;
						break;		
					case ITEM_DISPADJ_HUE_DOWN:
						if(Huevalue)
							Huevalue--;
							value = Huevalue+1;
							i = 2;
						break;
					}
				
		
					video_data [2] = (unsigned char )(0x08 + i);
					video_data [3] = (unsigned char )(0x00 + value);
					video_data [4] = (unsigned char )(0xEB + i+value);
		
					if (dev_SPIWrite(video_data,5)==0)
						return 0;//hangsheng 071126		
		}
		return -1;	
}
*/
static int send_data ( int index)
{
	int value=0,i=0;
	if ((index < 0) || (index > DISPADJ_BUTTON_QTY))
		return -1;
		if (index != 6)
			{
				switch(index)
				{
					case ITEM_DISPADJ_BRIGHT_UP:
						if(BrightValue < 10)
						BrightValue++;
						value = BrightValue+1;
							i = 0;
						break;
					case ITEM_DISPADJ_BRIGHT_DOWN:
						if(BrightValue)
							BrightValue--;
							value = BrightValue+1;
							i = 0;
						break;
					case ITEM_DISPADJ_COLOR_UP:
						if(ColorValue < 10)
							ColorValue++;
							value = ColorValue+1;
							i = 1;
						break;
					case ITEM_DISPADJ_COLOR_DOWN:
						if(ColorValue)
							ColorValue--;
							value = ColorValue+1;
							i = 1;
						break;
					case ITEM_DISPADJ_HUE_UP:
						if(Huevalue < 10)
							Huevalue++;
							value = Huevalue+1;
							i = 2;
						break;		
					case ITEM_DISPADJ_HUE_DOWN:
						if(Huevalue)
							Huevalue--;
							value = Huevalue+1;
							i = 2;
						break;
					}
				send_data_buf[0] =  HEAD;		
				send_data_buf[1] =  LENGTH;	
				send_data_buf [2] = (unsigned char )(IDDATA_BRI+ i);
				send_data_buf [3] = (unsigned char )(0x00 + value);
		
				return(send_data_out(send_data_buf,ALL_LENGTH));
							
		}	
		return -1;	
}


void VideoDispAdjInput(UInt32 key)
{
		int flag;
	
		if ( key == IRC_READ_INFO)
		{
			flag =message_dispatch ();
			if (flag == -1)
				printf ("main menu handle receive data error!\n");
			if (flag == 0)
				{
					//printf ("main menu handle receive data success!\n");
					return;
				}			
		}
		
	if( key == IRC_TOUCH_DOWN || key == IRC_TOUCH_UP )
	{
		int focus;
		TPosCoor pos;
		dt_ClipTouchGetXY( &pos.x, &pos.y );
		if( key == IRC_TOUCH_DOWN )
		{
			if( LastKey ==IRC_TOUCH_UP )
			{
				if( ( VideoDispAdjFocus = VideoDispAdjGetTouchID( pos )) != -1 )
				{
					VideoDispAdjDrawItem( VideoDispAdjFrame.pCanvas, VideoDispAdjFocus, TRUE );
					//dtpresent_flag++;		
					//pthread_cond_signal(&dt_cond);
					//dt_Present();
					dt_Present_part(VideoDispAdjTouchRect [VideoDispAdjFocus]);
					if ( send_data(VideoDispAdjFocus))
					printf ("write failure !\n");	
					//MenuVideoBtnEnter( MenuVideoFocus );
				}
			}
		}
		else
		{
			if( ( focus = VideoDispAdjGetTouchID( pos ) ) == VideoDispAdjFocus )
				VideoDispAdjBtnEnter( VideoDispAdjFocus );
			else if( VideoDispAdjFocus != -1 )
			{
				VideoDispAdjDrawItem( VideoDispAdjFrame.pCanvas, VideoDispAdjFocus, FALSE );
				//dtpresent_flag++;		
				//pthread_cond_signal(&dt_cond);
				//dt_Present();
				dt_Present_part(VideoDispAdjTouchRect [VideoDispAdjFocus]);
			}
			VideoDispAdjFocus = -1;
		}
		LastKey = key;//hangsheng add 071126
	}
	if(mute_status ==2) 
	VolumeCountHandle ();
}






